#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;
 
binmode STDOUT, ":utf8";
#use utf8;
 
use JSON;

require "doa_subs.pl";

############################## Main Prog ##############################

my $player_id={};
my $player_name={};
my $hashcnt1=0;
my $hashcnt2=0;

my $player_count=loadPlayers($player_id,$player_name);
$hashcnt1=keys %{$player_id};
$hashcnt2=keys %{$player_name};
print "initial player count $player_count playerid  $hashcnt1 player name $hashcnt2\n";

my @actFiles=readDir("activity");
#print Dumper(@actFiles);
foreach my $actfile (@actFiles)
{
	print "file $actfile\n";
	my $fullname="activity/".$actfile;
	my $data=readJSON($fullname);

	my $actarray = $data->{'dynamic'}{'players'}{'activity'};

	foreach my $actref (@$actarray)
	{
		print "pname $actref->{'pname'} aname $actref->{'aname'}\n";
    	$player_id->{$actref->{'pid'}}=$actref->{'pname'};
		$player_id->{$actref->{'aid'}}=$actref->{'aname'};
	}
}
$player_count=writePlayers($player_id);
$hashcnt1=keys %{$player_id};
$hashcnt2=keys %{$player_name};
print "after activity player count $player_count playerid  $hashcnt1 player name $hashcnt2\n";

my $alliance_id={};
my $alliance_name={};

my $alliance_count=loadAlliance($alliance_id,$alliance_name);
my @allFiles=readDir("adhocdata");
my $aname=@allFiles[scalar(@allFiles-1)];
my $aullname="adhocdata/".$aname;	# Get latest File only
print "file $aname\n";
my $allidata = readJSON($aullname);
my $alliref = $allidata->{'dynamic'}{'players'}{'alliances'};
my $foeref=$allidata->{'dynamic'}{'players'}{'foes'};
my $friendref=$allidata->{'dynamic'}{'players'}{'friends'};
my $aid="";
my $name="";
my $state="";
my $rank="";
my $lord="";
my $lord_id="";
my $members="";
my $matched="";

foreach my $allikey (@$alliref)
{
	$aid=$allikey->{'id'};
    $name=$allikey->{'name'};
	$rank=$allikey->{'rank'};
	$lord=$allikey->{'lord'};
	$lord_id=$allikey->{'lord_id'};
	$members=$allikey->{'count'};
	$state="";
	if ( ($matched) = grep $_ eq $aid, @$foeref)
    {
		$state="foe";
	}
	else 
	{
		if (($matched) = grep $_ eq $aid, @$friendref)
		{
			$state="friend";
		}
	}
#	print "$aid $name $state\n";
    $alliance_id->{$aid}={'name'=>$name, 'state'=>$state,'rank'=>$rank,'lord'=>$lord,'members'=>$members};
	print "lord_id $lord_id lord $lord\n";
	$player_id->{$lord_id}=$lord;
}
$hashcnt1=keys %{$player_id};
$hashcnt2=keys %{$player_name};
print "after lord load player count $player_count playerid  $hashcnt1 player name $hashcnt2\n";

my $playref=$allidata->{'map'}{'players'};
foreach my $play_id ( keys %{$playref})
{
	print "player_id $play_id name $playref->{$play_id}[0]\n";
	$player_id->{$play_id}=$playref->{$play_id}[0] unless $playref->{$play_id}[0] eq "0";
}

$hashcnt1=keys %{$player_id};
$hashcnt2=keys %{$player_name};
print "after plaer load player count $player_count playerid  $hashcnt1 player name $hashcnt2\n";

my $appref=$allidata->{'dynamic'}{'players'}{'applicants'};

foreach my $appkey (@$appref)
{
	$player_id->{$appkey->{'id'}}=$appkey->{'player'};
	print "applicant id $appkey->{'id'} name $appkey->{'player'}\n";
}
$hashcnt1=keys %{$player_id};
$hashcnt2=keys %{$player_name};
print "after applicant load player count $player_count playerid  $hashcnt1 player name $hashcnt2\n";

$alliance_count=writeAlliance($alliance_id);
$player_count=writePlayers($player_id);
$hashcnt1=keys %{$player_id};
$hashcnt2=keys %{$player_name};
print "after final write player count $player_count playerid  $hashcnt1 player name $hashcnt2\n";




